package com.jinly.ryder.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.common.error.SystemErrorType;
import com.jinly.ryder.manage.dao.HostInstanceMapper;
import com.jinly.ryder.manage.dao.entity.HostInstanceDO;
import com.jinly.ryder.manage.service.HostInstanceService;
import com.jinly.ryder.manage.service.mapping.HostInstanceMapping;
import com.jinly.ryder.manage.service.model.HostInstance;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class HostInstanceServiceImpl implements HostInstanceService {

    private final HostInstanceMapper hostInstanceMapper;
    private final HostInstanceMapping hostInstanceMapping;

    @Override
    public Page<HostInstance> instancePage(Page page, HostInstance query) {
        Wrapper<HostInstanceDO> queryWrapper = new QueryWrapper<HostInstanceDO>().lambda()
                .eq(query.getHost() != null, HostInstanceDO::getHost, query.getHost())
                .like(query.getName() != null, HostInstanceDO::getName, query.getName())
                .like(query.getOsName() != null, HostInstanceDO::getHost, query.getOsName());
        return hostInstanceMapping.toModelPage(hostInstanceMapper.selectPage(page, queryWrapper));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveHost(HostInstance instance) {
        if (ObjectUtils.isEmpty(instance.getName())) {
            instance.setName(instance.getHost());
        }
        if (instance.getId() == null) {
            HostInstance exist = getHostInstance(instance.getHost());
            if (exist != null) {
                SystemErrorType.BAD_REQUEST.throwError("主机已经存在");
            }
            log.info("新增主机:{}", instance);
            hostInstanceMapper.insert(hostInstanceMapping.toEntity(instance));
        } else {
            log.info("更新主机:{}", instance);
            hostInstanceMapper.updateById(hostInstanceMapping.toEntity(instance));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveHost(List<HostInstance> instanceList) {
        for (HostInstance hostInstance : instanceList) {
            saveHost(hostInstance);
        }
    }

    @Override
    public HostInstance getHostInstance(String host) {
        Wrapper<HostInstanceDO> queryWrapper = new QueryWrapper<HostInstanceDO>().lambda()
                .eq(HostInstanceDO::getHost, host)
                .last("limit 1");
        return hostInstanceMapping.toModel(hostInstanceMapper.selectOne(queryWrapper));
    }

    @Override
    public HostInstance getInstance(Integer hostId) {
        return hostInstanceMapping.toModel(hostInstanceMapper.selectById(hostId));
    }
}
